from apscheduler.schedulers.background import BackgroundScheduler
from append_save_kine import save_kline_data
import time


def sz002468_15m():
    """每分钟第55秒执行的目标方法"""
    # 调用函数
    stock_code = 'sz002468'
    frequency = '15m'
    save_kline_data(stock_code, frequency, count=8)
    print(f"任务执行于 {time.strftime('%Y-%m-%d %H:%M:%S')}")
def sh600406_15m():
    """每分钟第55秒执行的目标方法"""
    # 调用函数
    stock_code = 'sh600406'
    frequency = '15m'
    save_kline_data(stock_code, frequency, count=8)
    print(f"任务执行于 {time.strftime('%Y-%m-%d %H:%M:%S')}")
def sz002120_15m():
    """每分钟第55秒执行的目标方法"""
    # 调用函数
    stock_code = 'sz002120'
    frequency = '15m'
    save_kline_data(stock_code, frequency, count=8)
    print(f"任务执行于 {time.strftime('%Y-%m-%d %H:%M:%S')}")
def sh513180_5m():
    """每分钟第55秒执行的目标方法"""
    # 调用函数
    stock_code = 'sh513180'
    frequency = '5m'
    save_kline_data(stock_code, frequency, count=8)
    print(f"任务执行于 {time.strftime('%Y-%m-%d %H:%M:%S')}")
def sh513220_5m():
    """每分钟第55秒执行的目标方法"""
    # 调用函数
    stock_code = 'sh513220'
    frequency = '5m'
    save_kline_data(stock_code, frequency, count=8)
    print(f"任务执行于 {time.strftime('%Y-%m-%d %H:%M:%S')}")
def sz159612_5m():
    """每分钟第55秒执行的目标方法"""
    # 调用函数
    stock_code = 'sz159612'
    frequency = '5m'
    save_kline_data(stock_code, frequency, count=8)
    print(f"任务执行于 {time.strftime('%Y-%m-%d %H:%M:%S')}")
def sz159981_5m():
    """每分钟第55秒执行的目标方法"""
    # 调用函数
    stock_code = 'sz159981'
    frequency = '5m'
    save_kline_data(stock_code, frequency, count=8)
    print(f"任务执行于 {time.strftime('%Y-%m-%d %H:%M:%S')}")

# 创建后台调度器
scheduler = BackgroundScheduler()

# 添加多个调度任务覆盖所有时间段
scheduler.add_job(sz002468_15m, 'cron', hour='9', minute='30-59', second=55)  # 9:30-9:59
scheduler.add_job(sz002468_15m, 'cron', hour='10', minute='0-59', second=55)  # 10:00-10:59
scheduler.add_job(sz002468_15m, 'cron', hour='11', minute='0-30', second=55)  # 11:00-11:30
scheduler.add_job(sz002468_15m, 'cron', hour='13', minute='0-59', second=55)  # 13:00-13:59
scheduler.add_job(sz002468_15m, 'cron', hour='14', minute='0-59', second=55)  # 14:00-14:59

scheduler.add_job(sh513180_5m, 'cron', hour='9', minute='30-59', second=55)  # 9:30-9:59
scheduler.add_job(sh513180_5m, 'cron', hour='10', minute='0-59', second=55)  # 10:00-10:59
scheduler.add_job(sh513180_5m, 'cron', hour='11', minute='0-30', second=55)  # 11:00-11:30
scheduler.add_job(sh513180_5m, 'cron', hour='13', minute='0-59', second=55)  # 13:00-13:59
scheduler.add_job(sh513180_5m, 'cron', hour='14', minute='0-59', second=55)  # 14:00-14:59

scheduler.add_job(sh513220_5m, 'cron', hour='9', minute='30-59', second=55)  # 9:30-9:59
scheduler.add_job(sh513220_5m, 'cron', hour='10', minute='0-59', second=55)  # 10:00-10:59
scheduler.add_job(sh513220_5m, 'cron', hour='11', minute='0-30', second=55)  # 11:00-11:30
scheduler.add_job(sh513220_5m, 'cron', hour='13', minute='0-59', second=55)  # 13:00-13:59
scheduler.add_job(sh513220_5m, 'cron', hour='14', minute='0-59', second=55)  # 14:00-14:59

scheduler.add_job(sz159612_5m, 'cron', hour='9', minute='30-59', second=55)  # 9:30-9:59
scheduler.add_job(sz159612_5m, 'cron', hour='10', minute='0-59', second=55)  # 10:00-10:59
scheduler.add_job(sz159612_5m, 'cron', hour='11', minute='0-30', second=55)  # 11:00-11:30
scheduler.add_job(sz159612_5m, 'cron', hour='13', minute='0-59', second=55)  # 13:00-13:59
scheduler.add_job(sz159612_5m, 'cron', hour='14', minute='0-59', second=55)  # 14:00-14:59

scheduler.add_job(sh600406_15m, 'cron', hour='9', minute='30-59', second=55)  # 9:30-9:59
scheduler.add_job(sh600406_15m, 'cron', hour='10', minute='0-59', second=55)  # 10:00-10:59
scheduler.add_job(sh600406_15m, 'cron', hour='11', minute='0-30', second=55)  # 11:00-11:30
scheduler.add_job(sh600406_15m, 'cron', hour='13', minute='0-59', second=55)  # 13:00-13:59
scheduler.add_job(sh600406_15m, 'cron', hour='14', minute='0-59', second=55)  # 14:00-14:59

scheduler.add_job(sz159981_5m, 'cron', hour='9', minute='30-59', second=55)  # 9:30-9:59
scheduler.add_job(sz159981_5m, 'cron', hour='10', minute='0-59', second=55)  # 10:00-10:59
scheduler.add_job(sz159981_5m, 'cron', hour='11', minute='0-30', second=55)  # 11:00-11:30
scheduler.add_job(sz159981_5m, 'cron', hour='13', minute='0-59', second=55)  # 13:00-13:59
scheduler.add_job(sz159981_5m, 'cron', hour='14', minute='0-59', second=55)  # 14:00-14:59

scheduler.add_job(sz002120_15m, 'cron', hour='9', minute='30-59', second=55)  # 9:30-9:59
scheduler.add_job(sz002120_15m, 'cron', hour='10', minute='0-59', second=55)  # 10:00-10:59
scheduler.add_job(sz002120_15m, 'cron', hour='11', minute='0-30', second=55)  # 11:00-11:30
scheduler.add_job(sz002120_15m, 'cron', hour='13', minute='0-59', second=55)  # 13:00-13:59
scheduler.add_job(sz002120_15m, 'cron', hour='14', minute='0-59', second=55)  # 14:00-14:59


sz002468_15m()
sh513180_5m()
sh513220_5m()
sz159612_5m()
sh600406_15m()
sz159981_5m()
sz002120_15m()

scheduler.start()  # 启动调度器

# 保持主线程运行
try:
    while True:
        time.sleep(1)
except (KeyboardInterrupt, SystemExit):
    scheduler.shutdown()  # 优雅关闭